package Admin;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;

import java.awt.BorderLayout;
import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;

import com.example.finalproj.ChatData;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Chatting extends JInternalFrame {
	
	String fromid;
	String toid;
	HashMap<String, ServerThread> user;
	JTextArea ta_log;
	
	ChatData tc;
	
	private JPanel panel;
	private JScrollPane scrollPane;
	JTextArea ta_chatting;
	private JTextField tf_msg;
	private JButton btn_send;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Chatting frame = new Chatting();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Chatting() {
		setClosable(true);
		setResizable(true);
		setMaximizable(true);
		setIconifiable(true);
		setDefaultCloseOperation(JInternalFrame.HIDE_ON_CLOSE);
		setBounds(10, 10, 284, 215);
		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(getPanel(), BorderLayout.SOUTH);
		getContentPane().add(getScrollPane(), BorderLayout.CENTER);

	}
	public Chatting(String idto, HashMap<String, ServerThread> user, JTextArea ta_log)
	{
		this();
		this.ta_log = ta_log;
		this.user = user;
		this.fromid = idto;
		setTitle(this.fromid+" 님");
	}

	public JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getTf_msg());
			panel.add(getBtn_send(), BorderLayout.EAST);
		}
		return panel;
	}
	public JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTa_chatting());
		}
		return scrollPane;
	}
	public JTextArea getTa_chatting() {
		if (ta_chatting == null) {
			ta_chatting = new JTextArea();
			ta_chatting.setEditable(false);
		}
		return ta_chatting;
	}
	public JTextField getTf_msg() {
		if (tf_msg == null) {
			tf_msg = new JTextField();
			tf_msg.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {
					if(e.getKeyCode()==10)
					{
						sendconsol();
					}
				}
			});
			tf_msg.setColumns(10);
		}
		return tf_msg;
	}
	public void sendconsol()
	{
		ChatData c = new ChatData();
		c.setAction(2);
		c.setFromid(toid);
		c.setToid(fromid);
		c.setMsg(tf_msg.getText());
		tf_msg.setText("");
		try
		{
			user.get(fromid).oos.writeObject(c);
			Iterator<String> use = user.keySet().iterator();
			while(use.hasNext())
			{
				if(use.next().equals(toid))
				{
					user.get(toid).oos.writeObject(c);
					break;
				}
			}
			ta_log.append(c.getFromid()+" -> "+c.getToid()+" : "+c.getMsg()+"\n");
			ta_chatting.append(" -> "+c.getMsg()+"\n");
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	public JButton getBtn_send() {
		if (btn_send == null) {
			btn_send = new JButton("전송");
			btn_send.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					sendconsol();
				}
			});
		}
		return btn_send;
	}
	public void appMsg(ChatData cd)
	{
		this.setVisible(true);
		if(!cd.getGrade().equals("3"))
		{
			tc = cd;
		}
		if(toid==null)
		{
			toid = cd.getToid();
		}
		switch(cd.getAction())
		{
		case 2:
			ta_chatting.append(cd.getFromid()+" 님 : "+cd.getMsg()+"\n");
			break;
		case 4:
			ta_chatting.append("----- 공지 -----\n"+cd.getMsg()+"\n");
			break;
		}
		ta_chatting.setCaretPosition(ta_chatting.getText().length());
	}
	public void logoutMsg(ChatData cd)
	{
		ta_chatting.append(cd.getFromid()+" 님이 상담을 종료.\n");
		ta_chatting.setCaretPosition(ta_chatting.getText().length());
	}

	@Override
	public void doDefaultCloseAction() {
		// TODO Auto-generated method stub
		try
		{
			File f = new File("C:\\manage\\user\\"+toid+"\\"+fromid);
			f.mkdirs();
			File ff = new File(f.getPath()+"\\"+getTitle()+".txt");
			FileOutputStream fos = new FileOutputStream(ff);
			fos.write(ta_chatting.getText().getBytes());
			fos.close();
//			System.out.println(Arrays.toString(ab));
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
//		ta_chatting.append("없어지는중\n");
		super.doDefaultCloseAction();
	}
	
}
